
from sanic import Sanic
from sanic import response
from sanic.log import logger
from sanic_cors import CORS
from build_db import ExecuteSqlScript
from db_interface import ConnPG

from db_interface.class_table import ClassTable
from db_interface.brand_table import BrandTable
from db_interface.spu_table import SpuTable
from db_interface.spu_attribute_table import SpuAttributeTable
from db_interface.sku_table import SkuTable
from db_interface.sku_attribute_table import SkuAttributeTable
from db_interface.unit_table import UnitTable

app = Sanic(__name__)
CORS(app)

@app.listener('before_server_start')
async def setup_db(app, loop):
    #创建数据库，sql放在build_db文件夹下面
    ExecuteSqlScript().execute_all_script('build_db')

@app.listener('after_server_start')
async def start_db_pool(app, loop):
    ConnPG(loop=loop)
    logger.debug('Server PostgreSQL successfully started!')

@app.route("/")
async def test(request):
    return response.json({"hello": "world"})



@app.route("/add_class_ancestor")#添加祖节点，本系统为一级分类
async def tadd_ancestor(request):
    # return response.json({"hello": "world"})
    class_name = request.args.get('class_name')
    result = await ClassTable().add_ancestor(class_name)
    return response.json(result)

@app.route("/select_all_ancestor")#查看所有祖节点，本系统为一级分类
async def tadd_ancestor(request):
    result = await ClassTable().select_all_ancestor()
    return response.json(result)

@app.route("/add_class_son")
async def tadd_ancestor(request):#添加子节点点，本系统为二级分类

    class_name = request.args.get('class_name')
    parent_id = request.args.get('parent_id')
    result = await ClassTable().add_son(class_name, parent_id)
    # print(result)
    return response.json(result)

@app.route("/select_class")#查看节点的儿子节点
async def tadd_ancestor(request):

    class_name = request.args.get('class_name')
    result = await ClassTable().select_class(class_name)
    # print(result)
    return response.json(result)

@app.route("/select_class_son")#查看节点的儿子节点
async def tadd_ancestor(request):

    class_name = request.args.get('class_name')
    result = await ClassTable().select_son(class_name)
    # print(result)
    return response.json(result)

@app.route("/del_class", methods=['POST'])#删除class
async def del_class(request):

    class_id = request.form.get('class_id')

    result = await ClassTable().del_class(class_id)
    # print(result)
    return response.json(result)

@app.route("/update_class", methods=['POST'])#更新class_name
async def update_class(request):

    class_id = request.form.get('class_id')
    class_name = request.form.get('class_name')


    result = await ClassTable().update_class(class_id, class_name)
    return response.json(result)

    # print(result)

############# brand 表 #################

@app.route("/add_brand")#添加品牌
async def add_brand(request):

    brand_name = request.args.get('brand')
    result = await BrandTable().add_brand(brand_name)
    # print(result)
    return response.json(result)

@app.route("/select_all_brand")#查看所有品牌
async def select_all_brand(request):

    result = await BrandTable().select_all_brand()
    # print(result)
    return response.json(result)


@app.route("/del_brand", methods=['POST'])#删除brand
async def del_brand(request):

    brand_id = request.form.get('brand_id')

    result = await BrandTable().del_brand(brand_id)
    # print(result)
    return response.json(result)

@app.route("/update_brand", methods=['POST'])#更新brand_name
async def update_brand(request):

    brand_id = request.form.get('brand_id')
    brand_name = request.form.get('brand_name')


    result = await BrandTable().update_brand(brand_id, brand_name)
    # print(result)
    return response.json(result)

################# spu ######################

@app.route("/add_spu")#添加spu
async def add_spu(request):

    spu_name = request.args.get('spu_name')
    class_id = request.args.get('class_id')
    result = await SpuTable().add_spu(spu_name, class_id)
    # print(result)
    return response.json(result)

@app.route("/select_all_spu")#查看所有spu
async def select_all_spu(request):

    result = await SpuTable().select_all_spu()
    # print(result)
    return response.json(result)


@app.route("/del_spu", methods=['POST'])#删除spu
async def del_spu(request):

    spu_id = request.form.get('spu_id')

    result = await SpuTable().del_spu(spu_id)
    # print(result)
    return response.json(result)

@app.route("/update_spu", methods=['POST'])#更新spu_name
async def update_spu(request):

    spu_id = request.form.get('spu_id')
    spu_name = request.form.get('spu_name')


    result = await SpuTable().update_spu(spu_id, spu_name)
    # print(result)
    return response.json(result)

@app.route("/select_all_spu_by_class_id")#查看所有指定类的所有spu
async def select_all_spu_by_class_id(request):

    class_id = request.args.get('class_id')

    result = await SpuTable().select_all_spu_by_class_id(class_id)
    # print(result)
    return response.json(result)


@app.route("/add_spu_attribute")#添加spu属性
async def add_spu_attribute(request):

    spu_id = request.args.get('spu_id')
    attribute_key = request.args.get('attribute_key')
    attribute_value = request.args.get('attribute_value')
    result = await SpuAttributeTable().add_spu_attribute(spu_id, attribute_key, attribute_value)
    # print(result)
    return response.json(result)

@app.route("/select_spu_id_attribute")#查看指定spu
async def select_spu_id_attribute(request):
    spu_id = request.args.get('spu_id')
    result = await SpuAttributeTable().select_attribute(spu_id)
    return response.json(result)


@app.route("/del_spu_attribute", methods=['POST'])#删除spu_attribute
async def del_spu_attribute(request):

    spu_attribute_id = request.form.get('spu_attribute_id')

    result = await SpuAttributeTable().del_spu_attribute(spu_attribute_id)
    # print(result)
    return response.json(result)

@app.route("/update_spu_attribute", methods=['POST'])#更新spu_attribute_name
async def update_spu_attribute(request):

    spu_attribute_id = request.form.get('spu_attribute_id')
    spu_attribute_key = request.form.get('spu_attribute_key')
    spu_attribute_value = request.form.get('spu_attribute_value')

    result = await SpuAttributeTable().update_spu_attribute(spu_attribute_id, spu_attribute_key, spu_attribute_value)
    # print(result)
    return response.json(result)

    
################# sku 表 ######################

@app.route("/add_sku")#添加sku
async def add_sku(request):

    spu_id = request.args.get('spu_id')
    brand_id = request.args.get('brand_id')
    unit_id = request.args.get('unit_id')
    result = await SkuTable().add_sku(spu_id, brand_id, unit_id)
    # print(result)
    return response.json(result)

#没有参数
@app.route("/add_sku_attribute" )#添加sku属性
async def add_sku_attribute(request):
    sku_id = request.args.get('sku_id')
    attribute_id = request.args.get('attribute_id')
    # sku_id = request.form.get('sku_id')
    # attribute_id = request.form.get('attribute_id')
    result = await SkuAttributeTable().add_sku_attribute(sku_id, attribute_id)
    # print(result)
    return response.json(result)

@app.route("/del_sku_attribute", methods=['POST'])#删除sku属性
async def add_sku_attribute(request):
    sku_attribute_id = request.form.get('sku_attribute_id')
    result = await SkuAttributeTable().del_sku_attribute(sku_attribute_id)
    # print(result)
    return response.json(result)

@app.route("/select_sku_attribute")#查看指定sku的属性 #测试用例在test_add_sku_attribute下
async def select_sku_attribute(request):
    sku_id = request.args.get('sku_id')
    result = await SkuAttributeTable().select_attribute(sku_id)
    # print(result)
    return response.json(result)

@app.route("/select_sku")#查看指定sku_id的详情 #测试用例在test_add_sku_attribute下
async def select_sku(request):
    sku_id = request.args.get('sku_id')
    result = await SkuAttributeTable().select_sku_info(sku_id)
    # print(result)
    return response.json(result)

@app.route("/select_all_sku")#查看view_sku_info下的所有信息
async def select_sku(request):
    # sku_id = request.args.get('sku_id')
    result = await SkuTable().select_all_sku()
    # print(result)
    return response.json(result)

#未测试
@app.route("/del_sku", methods=['POST'])#删除sku
async def del_sku(request):

    sku_id = request.form.get('sku_id')

    result = await SkuTable().del_sku(sku_id)
    # print(result)
    return response.json(result)

################# add_unit ###################

@app.route("/add_unit")#添加单位
async def add_unit(request):

    unit = request.args.get('unit')
    result = await UnitTable().add_unit(unit)
    # print(result)
    return response.json(result)

@app.route("/select_all_unit")#查看单位
async def select_all_unit(request):

    result = await UnitTable().select_all_unit()
    # print(result)
    return response.json(result)


@app.route("/del_unit", methods=['POST'])#删除unit
async def del_unit(request):

    unit_id = request.form.get('unit_id')

    result = await UnitTable().del_unit(unit_id)
    # print(result)
    return response.json(result)

@app.route("/update_unit", methods=['POST'])#更新unit_name
async def update_unit(request):

    unit_id = request.form.get('unit_id')
    unit_name = request.form.get('unit_name')


    result = await UnitTable().update_unit(unit_id, unit_name)
    # print(result)
    return response.json(result)

if __name__ == "__main__":

    app.run(host="0.0.0.0", port=8000, debug=True)


